package com.lin.subject.domain.handler.subject;

import com.lin.subject.commom.enums.IsDeletedFlagEnum;
import com.lin.subject.commom.enums.SubjectInfoTypeEnum;
import com.lin.subject.domain.convert.MultipleSubjectConverter;
import com.lin.subject.domain.convert.RadioSubjectConverter;
import com.lin.subject.domain.entity.SubjectAnswerBO;
import com.lin.subject.domain.entity.SubjectInfoBO;
import com.lin.subject.domain.entity.SubjectOptionBO;
import com.lin.subject.infra.basic.entity.SubjectMultiple;
import com.lin.subject.infra.basic.entity.SubjectRadio;
import com.lin.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Author Lin
 * @Description 多选题实现类
 * @Date 2025/2/7
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    /**
     * 实际题目的插入
     *
     * @param subjectInfoBo
     */
    @Override
    public void insertSubject(SubjectInfoBO subjectInfoBo) {
        //多选题目的插入
        List<SubjectAnswerBO> optionList = subjectInfoBo.getOptionList();

        List<SubjectMultiple> radioList = optionList.stream().map(item -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            subjectMultiple.setOptionContent(item.getOptionContent());
            subjectMultiple.setOptionType(Long.valueOf(item.getOptionType()));
            subjectMultiple.setIsCorrect(item.getIsCorrect());
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            return subjectMultiple;
        }).collect(Collectors.toList());

        subjectMultipleService.insertList(radioList);
    }

    /**
     * 查询题目的选项及正确答案
     *
     * @param id
     * @return
     */
    @Override
    public SubjectOptionBO query(Long id) {
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();

        SubjectMultiple multiple = new SubjectMultiple();
        multiple.setSubjectId(id);
        List<SubjectMultiple> multipleList =  subjectMultipleService.queryListByCondition(multiple);
        List<SubjectAnswerBO> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.convertEntityToBoList(multipleList);
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }

    /**
     * 删除题目
     *
     * @param id
     */
    @Override
    public void deleteSubject(Long id) {
        subjectMultipleService.deleteSubject(id);
    }
}
